<template>
    <el-dialog title="Log Edit"
               :visible.sync="dialogVisiable">
        <el-form :model="form">
            <el-form-item label="Summary"
                          label-width="100">
                <el-input v-model="form.summary"
                          auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="Content"
                          label-width="100">
                <el-input v-model="form.content"
                          :rows="5"
                          type="textarea"
                          auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-date-picker v-model="form.day"
                                type="date"
                                format="yyyy-MM-dd"
                                :picker-options="datePickerOpt"
                                placeholder="Date">
                </el-date-picker>
            </el-form-item>
        </el-form>
        <span slot="footer"
              class="dialog-footer">
            <el-button @click="dialogVisiable= false">Cancel</el-button>
            <el-button type="primary"
                       @click="onLogDialogConfirm">OK</el-button>
        </span>
    </el-dialog>
</template>

<script>
    export default {
        data () {
            return {
                dialogVisiable: false,
                form: {
                    summary: '', content: '', day: '', id: '', isAdd: false
                },
                datePickerOpt: ''
            }
        },
        methods: {
            onLogDialogConfirm () {
                let json = {
                    'summary': this.form.summary,
                    'content': this.form.content,
                    'day': this.form.day,
                    'user': ''
                }

                let _id = this.form.id || ''
                if (_id) {
                    this.$nextTick(() => {
                        this.$axios.put(this.$api.API_LOGS + '/' + _id, json).then(res => {
                            this.loadData()
                        })
                    })
                } else {
                    this.$nextTick(() => {
                        this.$axios.post(this.$api.API_LOGS, json).then(res => {
                            this.loadData()
                        })
                    })
                }
            },
            loadData () {
                this.$emit('refresh')
                this.dialogVisiable = false
            }
        }
    }
</script>
